package cn.hwadee.muying.service;

import cn.hwadee.muying.dto.Worker;
import cn.hwadee.muying.entity.Recomend;
import cn.hwadee.muying.mapper.RecomendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @创建人 skiner
 * @创建时间 2018/8/13
 * @描述
 */
@Service
public class RecomendService {

    @Autowired
    private RecomendMapper recomendMapper;
    @Autowired
    private WorkerService workerService;
    public Recomend selectById(long userId)  {
        return recomendMapper.selectByUserId(userId);
    }

    public List<Worker> selectRecommendList(long userId){
        Recomend recomend=this.selectById(userId);
        List<Worker> workerList=new ArrayList<>();
        if(recomend!=null){
            Worker worker=null;
            if(recomend.getR1()!=null){
                worker=workerService.selectById(recomend.getR1());
                workerList.add(worker);
            }
            if(recomend.getR2()!=null){
                worker=workerService.selectById(recomend.getR2());
                workerList.add(worker);
            }
            if(recomend.getR3()!=null){
                worker=workerService.selectById(recomend.getR3());
                workerList.add(worker);
            }
            if(recomend.getR4()!=null){
                worker=workerService.selectById(recomend.getR4());
                workerList.add(worker);
            }
            if(recomend.getR5()!=null){
                worker=workerService.selectById(recomend.getR5());
                workerList.add(worker);
            }
            if(recomend.getR6()!=null){
                worker=workerService.selectById(recomend.getR6());
                workerList.add(worker);
            }
            if(recomend.getR7()!=null){
                worker=workerService.selectById(recomend.getR7());
                workerList.add(worker);
            }
            if(recomend.getR8()!=null){
                worker=workerService.selectById(recomend.getR8());
                workerList.add(worker);
            }
            if(recomend.getR9()!=null){
                worker=workerService.selectById(recomend.getR9());
                workerList.add(worker);
            }
            if(recomend.getR10()!=null){
                worker=workerService.selectById(recomend.getR10());
                workerList.add(worker);
            }
        }
        //随机选择5个相似进行推荐
        Collections.shuffle(workerList);
        if(workerList.size()>5){
            return  workerList.subList(0,5);
        }else{
            return workerList;
        }
    }
}
